<extend name="./Application/Admin/View/Layout/application.html"/>

<block name="content">
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品列表</strong> /
                    <small>Product</small>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{:U('Product/add')}}" class="am-btn am-btn-default"><span
                                    class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="am-btn am-btn-default del_all"><span
                                    class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>

                <form>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-form-group">
                            <select data-am-selected="{btnWidth: '50%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 360, searchBox: 1}"
                                    name="category_id">
                                <volist name="categories" id="category">
                                    <option value="">所有类别</option>
                                    <option value="{{$category.id}}" disabled>
                                        {{$category.name}}
                                        <volist name="category.children" id="child">
                                    <option value="{{$child.id}}">
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|--{{$child.name}}
                                    </option>
                                </volist>
                                </option>
                                </volist>

                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" name="name" class="am-form-field" placeholder="按商品名称">
                            <span class="am-input-group-btn">
                            <button class="am-btn am-btn-default" type="submit">搜索</button>
                            <a href="{{:U('Product/index')}}" class="am-btn am-btn-default">重置</a>
                            </span>
                        </div>
                    </div>
                </form>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th class="table-check"><input type="checkbox" class="checked_all"/></th>
                            <th class="table-id">ID</th>
                            <th class="table-title">商品名称</th>
                            <th class="table-type">所属分类</th>
                            <th class="table-author am-hide-sm-only">单价</th>
                            <th class="table-date am-hide-sm-only">库存</th>
                            <th class="table-date am-hide-sm-only">缩略图</th>
                            <th class="table-date am-hide-sm-only">置顶</th>
                            <th class="table-date am-hide-sm-only">新品</th>
                            <th class="table-date am-hide-sm-only">热销</th>
                            <th class="table-date am-hide-sm-only">推荐</th>
                            <th class="table-date am-hide-sm-only">上架</th>
                            <th class="table-date am-hide-sm-only">创建时间</th>
                            <th class="table-date am-hide-sm-only">商品排序</th>
                            <th class="table-set">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <volist name="products" id="product">
                            <tr data-id="{{$product.id}}">
                                <td><input type="checkbox" name="checked_id[]" class="checked_id"
                                           value="{{$product.id}}"/></td>
                                <td class="am-hide-sm-only">{{$product.id}}</td>
                                <td class="am-hide-sm-only">{{$product.name}}</td>
                                <td class="am-hide-sm-only"><a href="#">{{$product.category.name}}</a></td>
                                <td class="am-hide-sm-only">
                                    <input type="text" class="price" value="{{$product.price}}" style="width: 40px;">
                                </td>
                                <td class="am-hide-sm-only">
                                    <input type="text" class="stock_one" value="{{$product.stock}}"
                                           style="width: 40px;">
                                </td>
                                <td class="am-hide-sm-only">
                                    <img src="{{$product.thumb}}" style="width:80px;height:60px;">
                                </td>
                                <foreach name="datas" item="data">
                                    <td class="">
                                        {{:is_something($product,$data)}}
                                    </td>
                                </foreach>
                                <td class="am-hide-sm-only">{{$product.create_at|date='Y-m-d h:i:s',###}}</td>
                                <td>
                                    <input type="text" value="{{$product.sort_order}}" class="sort_order"
                                           style="width: 40px;">
                                </td>
                                <td>
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <a href="{{:U('edit', ['id'=>$product[id]])}}"
                                               class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                    class="am-icon-pencil-square-o"></span> 编辑
                                            </a>
                                            <a href="{{:U('cyble', ['id'=>$product[id]])}}"
                                               class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                <span class="am-icon-trash-o"></span> 回收
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </volist>
                        </tbody>
                    </table>
                    <div class="am-cf">
                        共 {{$count['products']}} 条记录
                        <div class="am-fr">
                            <ul class="am-pagination">
                                <!--<li class="am-disabled"><a href="#">«</a></li>-->
                                <!--<li class="am-active"><a href="#">1</a></li>-->
                                <!--<li><a href="#">2</a></li>-->
                                <!--<li><a href="#">3</a></li>-->
                                <!--<li><a href="#">4</a></li>-->
                                <!--<li><a href="#">5</a></li>-->
                                <!--<li><a href="#">»</a></li>-->
                                {{$pages}}
                            </ul>
                        </div>
                    </div>
                    <hr/>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
</block>

<block name="js">
    <script>
        $(function () {
            //全选/反选
            $('.checked_all').click(function () {
                $(':checkbox').prop("checked", this.checked);
            });

            $(".del_all").click(function () {
                var length = $(".checked_id:checked").length;
                if (length == 0) {
                    alert('您至少要选中一条记录');
                    return false;
                }

                if (confirm('确定要移至回收站么？')) {
                    var checked_id = $(".checked_id:checked").serialize();

                    $.ajax({
                        type: "POST",
                        url: "{{:U('delete_all')}}",
                        data: checked_id,
                        success: function (data) {
                            if (data.status == 1) {
                                alert(data.msg);
                                window.location.reload();
                            }
                        }
                    })
                }
                return false;
            })

            //AJAX排序
            $(".sort_order").change(function () {
                var id = $(this).parents('tr').data('id');
//                console.log(id);return false;
                var sort_order = $(this).val();
//                console.log(sort_order);return false;
                $.post("{{:U('sort_all')}}", {id: id, sort_order: sort_order}, function () {
//                        window.location.reload();
                })
                return false;
            })

            //5个是否AJAX修改属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
//                console.log(id);return false;
                var attr = $(this).attr('data-attr');
//                console.log(attr);return false;
                var _this = $(this);
                $.post("{{:U('change_attr')}}", {
                    id: id,
                    attr: attr
                }, function () {
                    _this.toggleClass('am-icon-check am-icon-close');
                })
                return false;
            })

            ///ajax修改库存
            $('.stock_one').change(function () {
                var id = $(this).parents('tr').data('id');
//                console.log(id);return false;
                var stock = $(this).val();
//                console.log(stock);return false;
                $.post("{{:U('stock_ajax')}}", {id: id, stock: stock}, function () {
//                    window.location.reload();

                })

            })

            /////ajax修改单价
            $('.price').change(function () {
                var id = $(this).parents('tr').data('id');
                var price = $(this).val();
                $.post("{{:U('price_ajax')}}", {id: id, price: price}, function () {
                    window.location.reload();
                })
            })
//            $.ajax({
//                type: "POST",
//                url: "{{:U('sort_all')}}",
//                data: "id,sort_order",
//                success: function (data) {
//                    if (data.status == 1) {
//                        alert(data.msg);
//                        window.location.reload();
//                    }
//                }
//            })

        })
    </script>
</block>

